﻿@using JavaDevToys.Rcl.Models
@using JavaDevToys.Rcl.Service
@using Microsoft.Extensions.DependencyInjection
@inherits ComponentBase;
<h3>设置</h3>
<MDialog @bind-Value="dialog"
        Transition="dialog-bottom-transition"
        MaxWidth="600">
<ChildContent>
    <MCard>
        <MToolbar Color="primary"
                    Dark>
            提示
        </MToolbar>
        <MCardText>
            <div class="text-h2 pa-12">保存成功!请重启应用或按F5刷新！</div>
        </MCardText>
        <MCardActions Class="justify-end">
            <MButton Text
                        OnClick="() => dialog = false">
                关闭
            </MButton>
        </MCardActions>
    </MCard>
</ChildContent>
</MDialog>
<MNavigationDrawer Value=Show ValueChanged=ShowChanged Fixed Right Temporary Width="800">
    <PrependContent>
        <MContainer>
            <MToolbar Style="width: 100%;" Absolute Elevation=0 Class="pr-4">
                <MSpacer></MSpacer>
                <MButton Icon OnClick="()=>{ Show = false; }">
                    <MIcon>mdi-close</MIcon>
                </MButton>
            </MToolbar>
        </MContainer>
    </PrependContent>
    <ChildContent>
        <div class="pa-6">
            <MToolbar Flat Color="blue-grey" Dark>
                <MToolbarTitle>设置</MToolbarTitle>
            </MToolbar>
            @foreach (var item in settingInfo.JavaProjectInfoList)
            {
                <MCard MaxHeight="800" Class="overflow-auto mt-6">
                    <MToolbar Flat Color="info">
                        <MToolbarTitle>
                            @item.Name
                        </MToolbarTitle>
                    </MToolbar>
                    <div class="d-flex ml-5 mr-5">
                        <MTextField @bind-Value="@item.FullPath" FullWidth="false" Readonly="true"
                                    Hint="This field uses counter prop"
                                    Label="@item.Name">
                        </MTextField>
                        <MButton class="mx-2"
                                 Fab
                                 Small
                                 Dark
                                 Color="primary" OnClick="()=>OpenFile(item)">
                            <MIcon>
                                mdi-folder
                            </MIcon>
                        </MButton>
                    </div>
                    <div class="d-flex">
                        <MCard MaxHeight="800" MaxWidth="800" Style="width:100%;" Class="overflow-auto ml-8 mr-8 mb-3">
                            <MToolbar Flat Color="primary">
                                <MToolbarTitle>
                                    数据库连接
                                </MToolbarTitle>
                                <MSpacer>
                                </MSpacer>
                            </MToolbar>
                            <div class="d-flex ma-5">
                                <MTextField @bind-Value="@item.sqlserverInfo.Server" FullWidth="false" Class="ml-1 mr-1" Readonly="true"
                                            Hint="This field uses counter prop"
                                            Label="服务器">
                                </MTextField>
                                <MTextField @bind-Value="@item.sqlserverInfo.Port" FullWidth="false" Class="ml-1 mr-1" Readonly="true"
                                            Hint="This field uses counter prop"
                                            Label="端口">
                                </MTextField>
                                <MTextField @bind-Value="@item.sqlserverInfo.Database" FullWidth="false" Class="ml-1 mr-1" Readonly="true"
                                            Hint="This field uses counter prop"
                                            Label="数据库">
                                </MTextField>
                                <MTextField @bind-Value="@item.sqlserverInfo.Schemas" FullWidth="false" Class="ml-1 mr-1" Readonly="true"
                                            Hint="This field uses counter prop"
                                            Label="Schemas">
                                </MTextField>
                            </div>
                            <MTextField @bind-Value="@item.sqlserverInfo.UserName" FullWidth="false" Class="ml-3 mr-3" Readonly="true"
                                        Hint="This field uses counter prop"
                                        Label="用户名">
                            </MTextField>
                            <MTextField @bind-Value="@item.sqlserverInfo.Pwd" FullWidth="false" Class="ml-3 mr-3" Readonly="true"
                                        Hint="This field uses counter prop"
                                        Label="密码">
                            </MTextField>
                        </MCard>
                    </div>
                </MCard>
            }
        </div>
        <div class="mb-2 mr-1">
            <MButton class="btn-float" Color="primary" OnClick="SaveAll">保存</MButton>
        </div>
    </ChildContent>
</MNavigationDrawer>
<style>
    .btn-float {
        position: fixed;
        bottom: 20px; 
        right: 20px; 
        padding: 15px 30px;
        background-color: #007bff;
        color: white;
        border: none;
        border-radius: 50px; 
        font-size: 16px;
        cursor: pointer;
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); /* Shadow effect */
    }

    .btn-float:hover {
        background-color: #0056b3; /* Darker on hover */
    }
</style>

@code {
    bool dialog = false;
    private bool _sticky;
    [Parameter]
    public bool? Show { get; set; }
    [Parameter]
    public EventCallback<bool?> ShowChanged { get; set; }
    private SettingInfo settingInfo;
    [Inject]
    public IJavaFileService javaFileService { get; set; }
    [Inject]
    public IServiceProvider serviceProvider { get; set; }
    [Inject]
    public IJavaProjectService javaProjectService { get; set; }
    [Inject]
    public ISettingService settingService { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        settingInfo=settingService.GetSettings();
    }
    private async void OpenFile(JavaProjectInfo javaProjectInfo)
    {
        try
        {
            var directoryPath = await serviceProvider.GetService<IDirectoryPicker>().PickDirectoryAsync();
            if (!string.IsNullOrEmpty(directoryPath))
            {
                javaProjectInfo.FullPath = directoryPath;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
        }
    }

    private void SaveAll()
    {
        try
        {
            settingService.SaveSettings(settingInfo);
            dialog = true;
        }
        catch (Exception e)
        {
        }
    }

}